package com.jonas.test;

import com.jonas.bean.Sport;
import com.jonas.mybatis.UserMapper;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import sun.jvm.hotspot.memory.Space;

import java.io.InputStream;
import java.util.List;
import java.util.stream.Stream;

/**
 * Created by myrna on 16/3/17.
 */
public class MybatisTestByXml {
    SqlSession sqlSession;
    @BeforeMethod
    public void setUp() throws Exception{
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
         sqlSession =  sqlSessionFactory.openSession();
    }

    @Test
    public void selectByXml() throws Exception{

       UserMapper sp =  sqlSession.getMapper(UserMapper.class);
        Sport s = sp.selectSport(1);
        Assert.assertEquals(s.getSportName(),"足球");
        Assert.assertEquals(s.getSportId(),1);
    }

    @Test
    public void selectSports() throws Exception{
        UserMapper sp =  sqlSession.getMapper(UserMapper.class);
        List<Sport> s = sp.getSports();
        Stream<Sport> sps = s.stream();
        sps.forEach(sss->{sss.getSportId();sss.getSportName();});
        for(Sport sq:s){
            System.out.println(sq.getSportId()+" "+sq.getSportName());
        }
        Assert.assertEquals(s.size(),10);
    }

    @Test
    public void saveSport() throws Exception{
        UserMapper sp =  sqlSession.getMapper(UserMapper.class);
        Sport sport = new Sport();
        sport.setSportId(25);
        sport.setSportName("test");
         sp.saveSport(sport);
        sqlSession.commit(); //
        sqlSession.close();
//        Assert.assertEquals(s.size(),10);
    }


    @Test
    public void deleteSport() throws Exception{
        UserMapper sp =  sqlSession.getMapper(UserMapper.class);
         sp.deleteSport(20);
        sqlSession.commit(); //
        sqlSession.close();
//        Assert.assertEquals(s.size(),10);
    }


}
